﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ShopOnline.Controls
{
    public class MyMenuItem
    {
        private string value;

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        } 
        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        private string link;

        public string Link
        {
            get { return link; }
            set { link = value; }
        }

        private bool visible;

        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        private bool last;

        public bool Last
        {
            get { return last; }
            set { last = value; }
        }

        System.Collections.Generic.List<MyMenuItem> items; 

        public System.Collections.Generic.List<MyMenuItem> Items
        {
            get { return items; }
            set { items = value; }
        }

        MyMenuItem parentItem;

        public MyMenuItem ParentItem
        {
            get { return parentItem; }
            set { parentItem = value; }
        }

        public string ToHTML()
        {
            string childHtml = "";
            if (items != null && items.Count > 0)
            {
                if (parentItem != null)
                    childHtml = "<ul>";
                else
                    childHtml = string.Format("<ul>");

                for (int i = 0; i < items.Count; i++)
                {
                    childHtml += items[i].ToHTML();
                }

                childHtml += "</ul>";
            }
            string itemHtml = ""; 
            if(parentItem != null)
                itemHtml = string.Format("<li><a class='level-top' href='{0}'><span>{1}</span></a>{2}</li>", link, text, childHtml);
            else
                itemHtml = string.Format("<li class='level-top parent{3}'><a class='level-top' href='{0}'><span>{1}</span></a>{2}</li>", string.IsNullOrEmpty(link) ? "" : link, text, childHtml, Last ? " last":"");

            return itemHtml;
        }

        public MyMenuItem(string text, string value, string link)
        {
            this.text = text;
            this.value = value;
            this.link = link;
            items = new System.Collections.Generic.List<MyMenuItem>();
            Last = false;
        }

        public MyMenuItem(string text, string value, string link, MyMenuItem parent)
        {
            this.text = text;
            this.value = value;
            this.link = link;
            this.parentItem = parent;
            parent.Items.Add(this);
            items = new System.Collections.Generic.List<MyMenuItem>();
            Last = false;
        }

        public MyMenuItem()
        {
            items = new System.Collections.Generic.List<MyMenuItem>();
            Last = false;
        }
    }

    public partial class MyMenuBar : System.Web.UI.UserControl
    {
        System.Collections.Generic.List<MyMenuItem> items;

        public System.Collections.Generic.List<MyMenuItem> Items
        {
            get { return items; }
            set { items = value; }
        }

        string cssClass;

        public string CssClass
        {
            get { return cssClass; }
            set { cssClass = value; }
        }

        public string ToHTML()
        {
            string childHtml = "";
            if (items != null && items.Count > 0)
            {
                for (int i = 0; i < items.Count; i++)
                {
                    childHtml += items[i].ToHTML();
                }
            }

            string html = string.Format("<ul id='nav'>{1}</ul>", ClientID, childHtml);
            return html;
        }

        public void Render()
        {
            litMenu.Text = ToHTML();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Render();  
            }
        }
    }
}